package com.ihisomp.controller;

import com.ihisomp.base.Result;
import com.ihisomp.dto.SysUserDto;
import com.ihisomp.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author zy
 * @since 2022-04-15
 */
@RestController
@Api(value = "机构用户信息", tags = "机构用户信息")
@RequestMapping("/sysUser")
public class SysUserController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @param sysUserDto
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true),
            @ApiImplicitParam(name = "size", value = "每页条数", required = true),
            @ApiImplicitParam(name = "sysUserDto", value = "机构用户数据类", required = true)})
    public Result search(@PathVariable("page") long page,
                         @PathVariable("size") long size,
                         @RequestBody(required = false) SysUserDto sysUserDto) {
        logger.info("分页查询：page={}，size={}, sysUserDto={}", page, size, sysUserDto);
        Result result = sysUserService.search(page, size, sysUserDto);
        return result;
    }

    /**
     * 新增数据
     *
     * @param sysUserDto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增数据")
    @ApiImplicitParam(name = "sysUserDto", value = "机构用户数据类", required = true)
    public Result add(@RequestBody SysUserDto sysUserDto) {
        logger.info("新增数据：sysUserDto={}", sysUserDto);
        Result result = sysUserService.add(sysUserDto);
        return result;
    }

    /**
     * 更新数据
     *
     * @param userKey
     * @param sysUserDto
     * @return
     */
    @PostMapping("/update/{userKey}")
    @ApiOperation("更新数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userKey", value = "机构用户主键", required = true),
            @ApiImplicitParam(name = "sysUserDto", value = "机构用户数据类", required = true)})
    public Result update(@PathVariable("userKey") String userKey, @RequestBody SysUserDto sysUserDto) {
        logger.info("更新用户信息：userKey={}, sysUserDto={}", userKey, sysUserDto);
        Result result = sysUserService.update(userKey, sysUserDto);
        return result;
    }

    /**
     * 查询数据
     *
     * @param userKey
     * @return
     */
    @GetMapping("/get/{userKey}")
    @ApiOperation("查询数据")
    @ApiImplicitParam(name = "userKey", value = "机构用户主键", required = true)
    public Result get(@PathVariable("userKey") String userKey) {
        logger.info("查询数据：userKey={}", userKey);
        Result result = sysUserService.get(userKey);
        return result;
    }

    /**
     * 更新用户状态
     *
     * @param userKey
     * @param activeFlg
     * @return
     */
    @PostMapping("status/{userKey}/{activeFlg}")
    @ApiOperation("更新用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userKey", value = "机构用户主键", required = true),
            @ApiImplicitParam(name = "activeFlg", value = "状态代码", required = true)})
    public Result updateStatus(@PathVariable("userKey") String userKey,
                               @PathVariable("activeFlg") int activeFlg) {
        logger.info("更新用户状态：userKey={}, activeFlg={}", userKey, activeFlg);
        Result result = sysUserService.updateStatus(userKey, activeFlg);
        return result;
    }

    /**
     * 修改用户密码
     *
     * @param sysUserDto
     * @return
     */
    @PostMapping("/updatePwd")
    @ApiOperation("修改用户密码")
    @ApiImplicitParam(name = "sysUserDto", value = "机构用户数据类", required = true)
    public Result updatePwd(@RequestBody SysUserDto sysUserDto) {
        logger.info("修改用户密码：sysUserDto={}", sysUserDto);
        Result result = sysUserService.updatePwd(sysUserDto);
        return result;
    }
}
